/**    
 * @Description: 博客园地址：https://www.cnblogs.com/zumengjie/      
 * @author: 顶风少年   
 * @date: 2021年4月5日 下午6:25:59
 */

package datang.dtdb.builder.xml;

import org.w3c.dom.Node;

import datang.dtdb.builder.BaseBuilder;
import datang.dtdb.builder.MapperBuilderAssistant;
import datang.dtdb.mapping.MappedStatement;
import datang.dtdb.mapping.SqlSource;
import datang.dtdb.mapping.StatementType;
import datang.dtdb.parsing.XPathParser;
import datang.dtdb.scripting.XMLScriptBuilder;
import datang.dtdb.session.Configuration;

/**
 * 创建Statement
 */
public class XMLStatementBuilder extends BaseBuilder {

	/**
	 * 可能是CRUD的一种
	 */
	private Node statementNode;

	/**
	 * mapper助理类
	 */
	MapperBuilderAssistant mapperBuilderAssistant;

	public XMLStatementBuilder(Configuration configuration, Node statementNode,
			MapperBuilderAssistant mapperBuilderAssistant) {
		super(configuration);
		this.mapperBuilderAssistant = mapperBuilderAssistant;
		this.statementNode = statementNode;
	}

	/**
	 * 解析具体的 CRUD标签，生成SqlSource
	 */
	public void parseStatementNode() {
		String resultType = XPathParser.eleAttr(statementNode, "resultType");
		Class<?> resolveAlias = null;
		if (resultType != null) {
			resolveAlias = resolveAlias(resultType);
		}
		String id = XPathParser.eleAttr(statementNode, "id");
		String statementTypeAttr = XPathParser.eleAttr(statementNode, "statementType");
		StatementType statementType;
		if (statementTypeAttr == null) {
			statementType = StatementType.PREPARED;
		} else {
			statementType = StatementType.valueOf(statementTypeAttr);
		}
		XMLScriptBuilder builder = new XMLScriptBuilder(configuration, statementNode);
		SqlSource sqlSource = builder.parseScriptNode();
		mapperBuilderAssistant.addMappedStatement(id, sqlSource, resolveAlias, statementType);
	}

}
